home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / sys / mac / mgetline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  6.8 KB  |  358 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)getline.c    3.1    90/22/02
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "Dialogs.h"
  7. #include <Packages.h>
  8.  
  9. // void FDECL(getlin,(const char *, char *));
  10. // void FDECL(get_ext_cmd,(char *));
  11. int FDECL ( try_key_queue , ( char * ) ) ;
  12.  
  13. char * FDECL ( PtoCstr , ( unsigned char * ) ) ;
  14. unsigned char * FDECL ( CtoPstr , ( char * ) ) ;
  15. void SetFrameItem ( DialogPtr , short , short ) ;
  16. void FlashButton ( DialogPtr , short ) ;
  17.  
  18. void FDECL ( enter_topl_mode , ( char * ) ) ;
  19. void FDECL ( leave_topl_mode , ( char * ) ) ;
  20. Boolean FDECL ( topl_key , ( unsigned char ) ) ;
  21. Boolean FDECL ( topl_ext_key , ( unsigned char ) ) ;
  22.  
  23. typedef Boolean FDECL ( ( * key_func ) , ( unsigned char ) ) ;
  24.  
  25.  
  26. int
  27. get_line_from_key_queue ( char * bufp )
  28. {
  29.     * bufp = 0 ;
  30.     if ( try_key_queue ( bufp ) ) {
  31.  
  32.         while ( * bufp ) {
  33.  
  34.             if ( * bufp == 10 || * bufp == 13 ) {
  35.  
  36.                 * bufp = 0 ;
  37.             }
  38.             bufp ++ ;
  39.         }
  40.         return true ;
  41.     }
  42.     return false ;
  43. }
  44.  
  45.  
  46. pascal Boolean
  47. getlinFilter ( DialogPtr dp , EventRecord * ev , short * itemHit )
  48. {
  49.     if (ev->what == keyDown) {
  50.         int key = ev->message & keyCodeMask,
  51.             ch    = ev->message & charCodeMask;
  52.  
  53.         if (ch == 0x1b || key == 0x3500 || key == 0x4700) {
  54.             *itemHit = 2;
  55.             FlashButton(dp, 2);
  56.             return true;
  57.  
  58.         } else if (ch == CHAR_CR || ch == CHAR_ENTER) {
  59.             *itemHit = 1;
  60.             FlashButton(dp, 1);
  61.             return true;
  62.         }
  63.     }
  64.  
  65.     return false;
  66. }
  67.  
  68.  
  69. void
  70. popup_getlin(const char *query, char *bufp)
  71. {
  72.     ControlHandle    ctrl;
  73.     DialogPtr        promptDialog;
  74.     short            itemHit, type;
  75.     Rect            box;
  76.     Str255            pasStr;
  77.  
  78.     if ( get_line_from_key_queue ( bufp ) )
  79.         return ;
  80.  
  81.     /*
  82.     ** Make a copy of the prompt string and convert the copy to a Pascal string.
  83.     */
  84.     
  85.     strcpy((char *) pasStr, query);
  86.     CtoPstr((char *) pasStr);
  87.     
  88.     /*
  89.     ** Set the query line as parameter text.
  90.     */
  91.     
  92.     ParamText(pasStr, "\p", "\p", "\p");
  93.     
  94.     promptDialog = mv_get_new_dialog(130);
  95.     ShowWindow(promptDialog);
  96.  
  97.     InitCursor ( ) ;
  98.     SetFrameItem ( promptDialog , 6 , 1 ) ;
  99.     do {
  100.         mv_modal_dialog(&getlinFilter, &itemHit);
  101.     } while ((itemHit != 1) && (itemHit != 2));
  102.     
  103.     if (itemHit != 2) {
  104.         /*
  105.         ** Get the text from the text edit item.
  106.         */
  107.         
  108.         GetDItem(promptDialog, 4, &type, (Handle *) &ctrl, &box);
  109.         GetIText((Handle) ctrl, pasStr);
  110.         
  111.         /*
  112.         ** Convert it to a 'C' string and copy it into the return value.
  113.         */
  114.         
  115.         PtoCstr(pasStr);
  116.         strcpy(bufp, (char *) pasStr);
  117.     } else {
  118.         /*
  119.         ** Return a null-terminated string consisting of a single <ESC>.
  120.         */
  121.         
  122.         bufp[0] = '\033';
  123.         bufp[1] = '\0';
  124.     }
  125.     
  126.     mv_close_dialog(promptDialog);
  127. }
  128.  
  129.  
  130. void
  131. topl_getlin(const char *query, char *bufp, key_func key)
  132. {
  133.     int q_len = strlen(query);
  134.  
  135.     if ( get_line_from_key_queue ( bufp ) )
  136.         return ;
  137.  
  138.     enter_topl_mode((char *) query);
  139.     while ((*key)(nhgetch())) ;
  140.     leave_topl_mode(bufp);
  141. }
  142.  
  143.  
  144. /*
  145.  * Read a line closed with '\n' into the array char bufp[BUFSZ].
  146.  * (The '\n' is not stored. The string is closed with a '\0'.)
  147.  * Reading can be interrupted by an escape ('\033') - now the
  148.  * resulting string is "\033".
  149.  */
  150. void
  151. mac_getlin(const char *query, char *bufp)
  152. {
  153.     if (flags.popup_dialog)
  154.         popup_getlin(query, bufp);
  155.     else
  156.         topl_getlin(query, bufp, &topl_key);
  157. }
  158.  
  159.  
  160. #ifdef COM_COMPL
  161.  
  162. pascal Boolean
  163. ExtendedCommandDialogFilter ( DialogPtr dp , EventRecord * ev , short * item )
  164. {
  165.     int ix ;
  166.     Handle h ;
  167.     Rect r ;
  168.     short k ;
  169.     Str255 s ;
  170.     unsigned char com [ 2 ] ;
  171.  
  172.     if ( ev -> what != keyDown ) {
  173.  
  174.         return 0 ;
  175.     }
  176.     com [ 0 ] = 1 ;
  177.     com [ 1 ] = ev -> message & 0xff ;
  178.  
  179.     if ( com [ 1 ] == 10 || com [ 1 ] == 13 || com [ 1 ] == 32 ||
  180.         com [ 1 ] == 3 ) { // various "OK"
  181.  
  182.         * item = 1 ;
  183.         FlashButton ( dp , 1 ) ;
  184.         return 1 ;
  185.     }
  186.     if ( com [ 1 ] == 27 || ( ev -> message & 0xff00 == 0x3500 ) ) { // escape
  187.  
  188.         * item = 2 ;
  189.         FlashButton ( dp , 2 ) ;
  190.         return 1 ;
  191.     }
  192.     for ( ix = 3 ; ix ; ix ++ ) {
  193.  
  194.         h = ( Handle ) NULL ;
  195.         k = 0 ;
  196.         GetDItem ( dp , ix , & k , & h , & r ) ;
  197.         if ( ! k || ! h ) {
  198.  
  199.             return 0 ;
  200.         }
  201.         if ( k == 6 ) {    //    Radio Button Item
  202.  
  203.             GetCTitle ( ( ControlHandle ) h , s ) ;
  204.             s [ 0 ] = 1 ;
  205.             if ( ! IUEqualString ( com , s ) ) {
  206.  
  207.                 * item = ix ;
  208.                 return 1 ;
  209.             }
  210.         }
  211.     }
  212. /*NOTREACHED*/
  213.     return 0 ;
  214. }
  215.  
  216.  
  217. void
  218. popup_get_ext_cmd(char *bufp)
  219. {
  220.     ControlHandle    ctrl;
  221.     DialogPtr        extendedDialog;
  222.     short            itemHit, type;
  223.     Rect            box;
  224.     char            *extendedCommand;
  225.  
  226.     /*
  227.     ** Default selection is the first item after the "Cancel" button.
  228.     */
  229.     static lastItemSelected = 3;
  230.  
  231.     if ( get_line_from_key_queue ( bufp ) )
  232.         return ;
  233.  
  234.     extendedDialog = mv_get_new_dialog(131);
  235.     ShowWindow(extendedDialog);
  236.  
  237.     /*
  238.     ** Mark the default selection.
  239.     */
  240.     
  241.     GetDItem(extendedDialog, lastItemSelected, &type, (Handle *) &ctrl, &box);
  242.     SetCtlValue(ctrl, 1);
  243.  
  244.     InitCursor ( ) ;
  245.     SetFrameItem ( extendedDialog , 20 , 1 ) ;
  246.     do {
  247.         mv_modal_dialog((ModalFilterProcPtr) ExtendedCommandDialogFilter , &itemHit);
  248.         if ((itemHit != 1) && (itemHit != 2)) {
  249.             /*
  250.             ** If OK and Cancel (items 1 and 2) weren't selected then a radio button 
  251.             ** was pushed.  Unmark the previous selection.
  252.             */
  253.             
  254.             GetDItem(extendedDialog, lastItemSelected, &type, (Handle *) &ctrl, &box);
  255.             SetCtlValue(ctrl, 0);
  256.             
  257.             /*
  258.             ** Mark the current selection.
  259.             */
  260.             
  261.             GetDItem(extendedDialog, itemHit, &type, (Handle *) &ctrl, &box);
  262.             SetCtlValue(ctrl, 1);
  263.  
  264.             /*
  265.             ** Save the item number for use later.
  266.             */
  267.             
  268.             lastItemSelected = itemHit;
  269.         }
  270.     } while ((itemHit != 1) && (itemHit != 2));
  271.     
  272.     if (itemHit == 2) {
  273.         /*
  274.         ** Return a null-terminated string consisting of a single <ESC>.
  275.         */
  276.         
  277.         bufp[0] = '\033';
  278.         bufp[1] = '\0';
  279.     } else {
  280.         switch (lastItemSelected) {
  281.         case 3:
  282.             extendedCommand = "adjust";
  283.             break;
  284.         case 4:
  285.             extendedCommand = "chat";
  286.             break;
  287.         case 5:
  288.             extendedCommand = "dip";
  289.             break;
  290.         case 6:
  291.             extendedCommand = "force";
  292.             break;
  293.         case 7:
  294.             extendedCommand = "jump";
  295.             break;
  296.         case 8:
  297.             extendedCommand = "loot";
  298.             break;
  299.         case 9:
  300.             extendedCommand = "monster";
  301.             break;
  302.         case 10:
  303.             extendedCommand = "name";
  304.             break;
  305.         case 11:
  306.             extendedCommand = "offer";
  307.             break;
  308.         case 12:
  309.             extendedCommand = "pray";
  310.             break;
  311.         case 13:
  312.             extendedCommand = "rub";
  313.             break;
  314.         case 14:
  315.             extendedCommand = "sit";
  316.             break;
  317.         case 15:
  318.             extendedCommand = "turn";
  319.             break;
  320.         case 16:
  321.             extendedCommand = "untrap";
  322.             break;
  323.         case 17:
  324.             extendedCommand = "version";
  325.             break;
  326.         case 18:
  327.             extendedCommand = "window";
  328.             break;
  329.         case 19:
  330.             extendedCommand = "wipe";
  331.             break;
  332.         }
  333.         
  334.         /*
  335.         ** Copy the text representing the last radio button selected into the buffer.
  336.         */
  337.         
  338.         strcpy(bufp, extendedCommand);
  339.     }
  340.     
  341.     mv_close_dialog(extendedDialog);
  342. }
  343.  
  344.  
  345. /* Read in an extended command - doing command line completion for
  346.  * when enough characters have been entered to make a unique command.
  347.  * This is just a modified getlin().   -jsb
  348.  */
  349. void
  350. mac_get_ext_cmd(char *bufp)
  351. {
  352.     topl_getlin("# ", bufp, &topl_ext_key);
  353. }
  354.  
  355. #endif /* COM_COMPL /* */
  356.  
  357. /* macgetline.c */
  358.